package com.midea.ai.air.ui.home.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.ui.home.adapter.SelectRoomAdapter;
import com.midea.ai.air.ui.home.data.SelectRoomModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/25.
 * 添加家电->选择房间Fragment
 */
public class AddDeviceSelectRoomFragment extends BaseFragment {
    private ListView mListRooms;
    private SelectRoomAdapter mRoomAdapter;
    private List<SelectRoomModel> mRooms;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_device_select_room, container, false);
        initData();
        initViews(view);
        return view;
    }

    private void initData() {
        mRooms = new ArrayList<>();
        mRooms.add(new SelectRoomModel("客厅", true));
        mRooms.add(new SelectRoomModel("主卧", false));
        mRooms.add(new SelectRoomModel("次卧", false));
        mRoomAdapter = new SelectRoomAdapter(getActivity(), 0, mRooms);
    }

    private void initViews(View view) {
        mListRooms = (ListView) view.findViewById(R.id.list_rooms);
        mListRooms.setAdapter(mRoomAdapter);
        mListRooms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(!mRooms.get(position).isSelected()) {
                    mRooms.get(position).setSelected(true);
                    for(int i=0; i<mRooms.size(); i++) {
                        if(i!=position) {
                            mRooms.get(i).setSelected(false);
                        }
                    }
                }
                mRoomAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 外部新建房间时调用该方法
     * @param room 房间名
     */
    public void newRoom(String room) {
        if(mRooms != null) {
            mRooms.add(new SelectRoomModel(room, false));
        } else {
            mRooms = new ArrayList<>();
            mRooms.add(new SelectRoomModel(room, false));
        }
    }
}
